package handler

import (
	"context"
	"eat-server/global"
	"eat-server/models"
	__ "eat-server/proto"
	"fmt"
)

func (s *Server) Sendsms(_ context.Context, in *__.SendsmsRequest) (*__.SendsmsResponse, error) {

	//code:=rand.Intn(9000)+1000
	//
	//get:=inits.Rdb.Get(context.Background(),"sendsms" + in.Code+ "register")
	//if get.Val()!=""{
	//
	//}
	return &__.SendsmsResponse{}, nil
}

func (s *Server) Register(_ context.Context, in *__.RegisterRequest) (*__.RegisterResponse, error) {
	find := models.Users{}
	global.DB.Where("tel = ?", in.Tel).Find(&find)
	//未注册过的自动注册
	user := models.Users{
		Tel:      in.Tel,
		Password: in.Password,
	}
	global.DB.Create(&user)

	return &__.RegisterResponse{
		Id: user.Id,
	}, nil
}
func (s *Server) Login(_ context.Context, in *__.LoginRequest) (*__.LoginResponse, error) {
	find := models.Users{}
	global.DB.Where("tel = ?", in.Tel).Find(&find)
	if find.Password != in.Password || find.Tel != in.Tel {
		return nil, fmt.Errorf("账号或密码错误 请重新输入")
	}
	return &__.LoginResponse{
		Id: find.Id,
	}, nil
}
